import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  videoQuestionList: {},
  currentVideoQuestion: {},
  keys: ["A", "B", "C", "D", "E", "F", "G", "H"],
  searchVideoQuestionCondition: {
    pageSize: 10,
    title: ""
  }
};

const getters = {
  keys: state => state.keys,
  videoQuestionList: state => state.videoQuestionList,
  currentVideoQuestion: state => state.currentVideoQuestion,
  searchVideoQuestionCondition: state => state.searchVideoQuestionCondition
};

const actions = {
  getVideoQuestionPageS({ commit }, param) {
    return Api.getVideoQuestionPage(param).then(ret => {
      commit(types.UPDATE_VIDEOQUESTION_LIST, ret.data.data);
      return ret;
    });
  },
  getVideoQuestionS({ commit }, id) {
    return Api.getVideoQuestion({ id: id }).then(ret => {
      var payload = ret.data.data;
      var opts = { opts: [] };
      payload = Object.assign(payload, opts);
      var tem = payload.options.split("\n");
      console.log(tem);
      var ans = payload.answer.split("、");
      if (tem) {
        tem.map((item, index) => {
          var chek = false;
          ans.map((s, d) => {
            if (s === state.keys[index]) {
              chek = true;
            }
          });
          payload.opts.push({
            value: item.substring(2, item.length),
            checked: chek,
            key: state.keys[index]
          });
        });
      }
      commit(types.UPDATE_CURRENT_VIDEOQUESTION, payload);
      return ret;
    });
  },
  addOrUpdateVideoQuestionS({ commit }, payload) {
    return Api.addOrUpdateVideoQuestion({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteVideoQuestionS({ commit }, param) {
    return Api.deleteVideoQuestion(param).then(ret => {
      return ret;
    });
  }
};
const mutations = {
  [types.UPDATE_VIDEOQUESTION_LIST](state, payload) {
    state.videoQuestionList = payload;
  },
  [types.INIT_CURRENT_VIDEOQUESTION](state, payload) {
    state.currentVideoQuestion = {
      title: "",
      options: "",
      opts: [
        {
          value: "",
          checked: false,
          key: "A"
        }
      ],
      answer: "",
      analysis: "",
      score: 1
    };
  },
  [types.UPDATE_CURRENT_VIDEOQUESTION](state, payload) {
    state.currentVideoQuestion = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
